require 'base64'
require 'openssl'
require 'digest'
require 'securerandom'

class UserAPI < Grape::API
	#员工相关的接口
	$redis = Redis.new()
	version 'v1', using: :path
	format :json
	# AVATAR_IMAGE = 'http://7xvwdu.com1.z0.glb.clouddn.com/avatar.jpeg'
		desc '获取用户信息'
		params do
	      optional :id, type: String, desc: '用户ID'
	    end

		get 'user' do
			if params[:id].nil?
				{code: '400', msg: '请输入用户id'}
			else
				user = User.where(id: params[:id]).first
				if user.blank?
					{code: '400', message: '用户不存在'}
				else
					user_detail = UserDetail.where(user_id: user.id).first
					if user_detail.project_id.nil?
						project_name = ""
					else
						project = Project.where(id: user_detail.project_id).first
						if project.nil?
							project_name = ""
						else
							project_name = project.name
						end
					end
					# score = ((user_detail.score01.nil? ? 0 : user_detail.score01) + (user_detail.score02.nil? ? 0 : user_detail.score02) + (user_detail.score03.nil? ? 0 : user_detail.score03))/3
					# if score == 0
					# 	score = -1
					# end
					company =  Company.where(id: user_detail.company_id).first
					if company.blank?
						company_name = ""
					else
						company_name = company.name
					end
					if user_detail.gender == 0
						gender = ""
					elsif user_detail.gender == 1
						gender = "男"
					else
						gender = "女"
					end
					if user_detail.validate_type == 2
						validate_type = "已认证"
					elsif user_detail.validate_type == 1
						validate_type = "审核中"
					else
						validate_type = "未认证"
					end
					score = UserAPI.user_score(user.id)
					{
						code: '0',
						msg: 'OK',
						user_info:{
							'user_id': user.id,
							'user_name': user.name.nil? ? "" : user.name,
							'gender': gender,
							'phone': user.phone.nil? ? "" : user.phone,
							"is_admin": user.admin_status,
							'validate_type': validate_type,
							'identity_card': user.identity_card.nil? ? "" : CommonApi::des_encrypt(user.identity_card),
							'birthday': user_detail.birthday.nil? ? "" : CommonApi::format_time(user_detail.birthday),
							'ethnicity': user_detail.ethnicity.nil? ?  "" : CommonApi::cover_ethnicity(user_detail.ethnicity),
							'native_place': user_detail.native_place.nil? ? "" : CommonApi::cover_city(user_detail.native_place),
							'job': user_detail.job_id.nil? ? "" : CommonApi::cover_job(user_detail.job_id),
							'company': company_name,
							'score': score,
							'experience': user_detail.experience.nil? ? "" : user_detail.experience,
							'head_image': user_detail.head_img_url.nil? ? "" : user_detail.head_img_url,
							'project_id': user_detail.project_id.nil? ? -1 : user_detail.project_id,
							'project_name': project_name,
							'real_name_status': user_detail.real_name_status,
							'pay_card': user_detail.pay_card.nil? ? "" : user_detail.pay_card
						}
					}
				end
			end
		end

		desc '注册功能'
		params do
			optional :phone, type: Integer, desc: '电话号码'
			optional :passwd, type: String, desc: '密码'
			optional :confirm_passwd, type: String, desc: '确认密码'
			optional :sms_code, type:String, desc: '验证码'
			optional :identity_card, type: String, desc: '身份证号' #, regexp: /(^\d{15}$)|(^\d{17}([0-9]|X))$/
		end
		post 'register' do
			content_type 'application/x-www-form-urlencoded'
			if params[:phone].nil? || (/^1[345678]\d{9}$/ =~ params[:phone].to_s).nil? 
				{code: '400', msg: '请输入正确的手机号'}
			# elsif params[:identity_card].nil?  # 缺了身份证验证
			# 	{code: '400', msg: '请输入身份证号'}
			elsif params[:sms_code].nil?
				{code: '400', msg: '请输入验证码'}
			elsif params[:passwd].nil? ||  params[:confirm_passwd].nil?
				{code: '400', msg: '请输入密码'}
			else
				if params[:sms_code].eql?($redis.get(params[:phone]).to_s)
					$redis.del(params[:phone])
					# if params[:identity_card].length % 16 == 0  && params[:passwd].length % 16 ==0	
						if params[:identity_card].nil?
							if params[:passwd].eql?(params[:confirm_passwd])
								passwd = CommonApi::des_decrypt(params[:passwd])
								user = User.new
								user.phone = params[:phone]
								user.encrypted_password = Digest::MD5.hexdigest(passwd)
								# user.identity_card = identity_card
								user.user_type = '1'
								user.name = params[:phone]
								user_detail = UserDetail.new
								if User.where(phone: params[:phone]).first.blank?
									user.save
									user_detail.user_id = user.id
									user_detail.save
									{code: '0', msg: 'OK', user_id: user.id}
								else
									{code: '400', msg: '手机号已注册'}
								end
							else
								{code: '400', msg: '密码不一致'}
							end
						else
							if params[:passwd].eql?(params[:confirm_passwd])
								identity_card = CommonApi::des_decrypt(params[:identity_card])
								if !((/(^\d{15}$)|(^\d{17}([0-9]|X))$/ =~ identity_card.to_s).nil?)
									passwd = CommonApi::des_decrypt(params[:passwd])
									user = User.new
									user.phone = params[:phone]
									user.encrypted_password = Digest::MD5.hexdigest(passwd)
									user.identity_card = identity_card
									user.user_type = '1'
									user.name = params[:phone]
									user_detail = UserDetail.new
									if (User.where(identity_card: identity_card).first.blank? && User.where(phone: params[:phone]).first.blank?)
										user.save
										user_detail.user_id = user.id
										user_detail.real_name_status = 0
										user_detail.save
										{code: '0', msg: 'OK', user_id: user.id}
									else
										{code: '400', msg: '身份证号或手机号已注册'}
									end
								else
									{code: '400', msg: '身份证格式错误'}
								end
							else
								{code: '400', msg: '密码不一致'}
							end
						end
					# else
					# 	{code: '400', msg: '密码或身份证号格式不正确'}
					# end
				else
					{code: '400', msg: '验证码错误'}
				end
			end
		end
		#######找回密码接口
		desc '找回密码'
		params do
			optional :phone, type: Integer, desc: '电话号码'
			optional :passwd, type: String, desc: '密码'
			optional :confirm_passwd, type: String, desc: '确认密码'
			optional :sms_code, type:String, desc: '验证码'
		end

		post 'reset_passwd' do
			content_type 'application/x-www-form-urlencoded'
			if params[:phone].nil?
				{code: '400', msg: '请输入手机号'}
			elsif params[:sms_code].nil?
				{code: '400', msg: '请输入验证码'}
			elsif params[:passwd].nil? ||  params[:confirm_passwd].nil?
				{code: '400', msg: '请输入密码'}
			else
				if params[:sms_code].eql?($redis.get(params[:phone]).to_s)
				  	$redis.del(params[:phone])# if params[:confirm_passwd].length % 16 == 0  && params[:passwd].length % 16 ==0
						if (/^1[345678]\d{9}$/ =~ params[:phone].to_s).nil? # 验证手机号码是否正确
							{code: '400', msg: '手机号码不正确'}
						else
							if params[:passwd].eql?(params[:confirm_passwd])
								passwd = CommonApi::des_decrypt(params[:passwd])
								# passwd = params[:passwd]
								user  = User.where(phone: params[:phone]).first
								if (user.blank?)
									{code: '400', msg: '没有查找到用户'}
								else
									user.encrypted_password = Digest::MD5.hexdigest(passwd)
									if user.save
										{code: '0', msg: 'OK'}
									else
										{code: '400', msg: '数据错误'}
									end
								end
							end
						end
					# else
					# 	{code: '400', msg: '密码格式错误'}
					# end
				else
					{code: '400', msg: '验证码错误'}
				end
			end
		end

		desc '登录功能'
		params do
			optional :user_name, type: String, desc: '登录用户名(手机号/身份证号)'
			optional :passwd, type: String, desc: '密码'
		end
		post 'login' do
			content_type 'application/x-www-form-urlencoded'
			if params[:user_name].nil?
				{code: '400', msg: '请输入手机号或身份证号'}
			elsif params[:passwd].nil?
				{code: '400', msg: '请输入密码'}
			else
				passwd = Digest::MD5.hexdigest(CommonApi::des_decrypt(params[:passwd]))
				if (/(^\d{15}$)|(^\d{17}([0-9]|X))$/ =~ params[:user_name].to_s) == 0
					user = User.where(identity_card: params[:user_name], encrypted_password: passwd).first
					# user = User.where(identity_card: identity_card).first
					if user.nil?
						{code: '400', msg: '用户名或密码错误'}
					elsif user.publish_status == 0
						{code: '400', msg: '用户已禁用'}
					else
						user_detail = UserDetail.where(user_id: user.id).first
						if user_detail.project_id.nil?
							project_name = ""
						else
							project = Project.where(id: user_detail.project_id).first
							if project.nil?
								project_name = ""
							else
								project_name = project.name
							end
						end
						score = UserAPI.user_score(user.id)
						company =  Company.where(id: user_detail.company_id).first
						if company.blank?
							company_name = ""
						else
							company_name = company.name
						end
						if user_detail.gender == 0
							gender = ""
						elsif user_detail.gender == 1
							gender = "男"
						else
							gender = "女"
						end
						if user_detail.validate_type == 2
							validate_type = "已认证"
						elsif user_detail.validate_type == 1
							validate_type = "审核中"
						else
							validate_type = "未认证"
						end
						{
							code: '0',
							msg: 'OK',
							user_info:{
								'user_id': user.id,
								'user_name': user.name.nil? ? "" : user.name,
								'gender': gender,
								'phone': user.phone.nil? ? "" : user.phone,
								"is_admin": user.admin_status,
								'validate_type': validate_type,
								'identity_card': user.identity_card.nil? ? "" : CommonApi::des_encrypt(user.identity_card),
								'birthday': user_detail.birthday.nil? ? "" : CommonApi::format_time(user_detail.birthday),
								'ethnicity': user_detail.ethnicity.nil? ? "" : CommonApi::cover_ethnicity(user_detail.ethnicity),
								'native_place': user_detail.native_place.nil? ? "" : CommonApi::cover_city(user_detail.native_place),
								'job': user_detail.job_id.nil? ? "" : CommonApi::cover_job(user_detail.job_id),
								'company': company_name,
								'score': score,
								'experience': user_detail.experience.nil? ? "" : user_detail.experience,
								'head_image': user_detail.head_img_url.nil? ? "" : user_detail.head_img_url,
								'project_id': user_detail.project_id.nil? ? -1 : user_detail.project_id,
								'project_name': project_name,
								'pay_card': user_detail.pay_card.nil? ? "" : user_detail.pay_card
							}
						}
					end
				elsif (/^1[345678]\d{9}$/ =~ params[:user_name].to_s) == 0 # 验证手机号码是否正确
					user = User.where(phone: params[:user_name], encrypted_password: passwd).first
					if user.nil?
						{code: '400', msg: '用户名或密码错误'}
					else
						user_detail = UserDetail.where(user_id: user.id).first
						if user_detail.project_id.nil?
							project_name = ""
						else
							project = Project.where(id: user_detail.project_id).first
							if project.nil?
								project_name = ""
							else
								project_name = project.name
							end
						end
						score = UserAPI.user_score(user.id)
						company =  Company.where(id: user_detail.company_id).first
						if company.blank?
							company_name = ""
						else
							company_name = company.name
						end
						if user_detail.gender == 0
							gender = ""
						elsif user_detail.gender == 1
							gender = "男"
						else
							gender = "女"
						end
						if user_detail.validate_type == 2
							validate_type = "已认证"
						elsif user_detail.validate_type == 1
							validate_type = "审核中"
						else
							validate_type = "未认证"
						end
						{
							code: '0',
							msg: 'OK',
							user_info:{
								'user_id': user.id,
								'user_name': user.name.nil? ? "" : user.name,
								'gender': gender,
								'phone': user.phone.nil? ? "" : user.phone,
								"is_admin": user.admin_status,
								'validate_type': validate_type,
								'identity_card': user.identity_card.nil? ? "" : CommonApi::des_encrypt(user.identity_card),
								'birthday': user_detail.birthday.nil? ? "" : CommonApi::format_time(user_detail.birthday),
								'ethnicity': user_detail.ethnicity.nil? ? "" : CommonApi::cover_ethnicity(user_detail.ethnicity),
								'native_place': user_detail.native_place.nil? ? "" : CommonApi::cover_city(user_detail.native_place),
								'job': user_detail.job_id.nil? ? "" : CommonApi::cover_job(user_detail.job_id),
								'company': company_name,
								'score': score,
								'experience': user_detail.experience.nil? ? "" : user_detail.experience,
								'head_image': user_detail.head_img_url.nil? ? "" : user_detail.head_img_url,
								'project_id': user_detail.project_id.nil? ? -1 : user_detail.project_id,
								'project_name': project_name,
								'pay_card': user_detail.pay_card.nil? ? "" : user_detail.pay_card
							}
						}
					end
				else
					{code: '400', msg: '请输入正确的手机号或身份证号'}
				end
			end
		end


		desc '修改用户信息'
		params do
			optional :user_name, type: String, desc: '登录用户名(手机号/身份证号)'
			optional :passwd, type: String, desc: '密码'
			optional :name, type: String, desc: '工作人员名字'
			optional :gender, type: Integer, desc: '性别'
			optional :birthday, type: Integer, desc: '生日'
			optional :ethnicity, type: Integer, desc: '民族'
			optional :job, type: Integer, desc: '工种'
			optional :company, type: Integer, desc: '劳务公司'
			optional :experience, type: String, desc: '工作经历'
			optional :pay_card, type: String, desc: '工资卡号'
		end
		post 'edit_user' do
			if params[:user_name].nil?
				{code: '400', msg: '请输入手机号或身份证号'}
			elsif params[:passwd].nil?
				{code: '400', msg: '请输入密码'}
			else
				passwd = Digest::MD5.hexdigest(CommonApi::des_decrypt(params[:passwd]))
				if (/(^\d{15}$)|(^\d{17}([0-9]|X))$/ =~ params[:user_name].to_s) == 0
					user = User.where(identity_card: identity_card, encrypted_password: passwd).first
				  # user = User.where(identity_card: identity_card).first
				else
					user = User.where(phone: params[:user_name], encrypted_password: passwd).first
					# user = User.where(phone: params[:user_name]).first
				end
				if user.nil?
					{code: '400', msg: '用户名或密码错误'}
				else
					user_detail = UserDetail.where(user_id: user.id).first
					if user_detail.project_id.nil?
						project_name = ""
					else
						project = Project.where(id: user_detail.project_id).first
						if project.nil?
							project_name = ""
						else
							project_name = project.name
						end
					end
					if !(params[:name].nil?)
						user.name = params[:name]
					end
					if !(params[:gender].nil?)
						user_detail.gender = params[:gender]
					end
					if !(params[:birthday].nil?)
						user_detail.birthday = params[:birthday]
					end
					if !(params[:ethnicity].nil?)
						user_detail.ethnicity = params[:ethnicity]
					end
					if !(params[:native_place].nil?)
						user_detail.native_place = params[:native_place]
					end
					if !(params[:job].nil?)
						user_detail.job_id = params[:job]
					end
					if !(params[:experience].nil?)
						user_detail.experience = params[:experience]
					end
					if !(params[:company].nil?)
						company =  Company.where(id: params[:company]).first
						if company.blank?
							company_name = ""
						else
							user_detail.company_id = params[:company]
							company_name = company.name
						end
					else
						company =  Company.where(id: user_detail.company_id).first
						if company.blank?
							company_name = ""
						else
							company_name = company.name
						end
					end
					user_detail.pay_card = params[:pay_card]
					if user_detail.save && user.save
						score = UserAPI.user_score(user.id)

						if user_detail.gender == 0
							gender = ""
						elsif user_detail.gender == 1
							gender = "男"
						else
							gender = "女"
						end
						if user_detail.validate_type == 2
							validate_type = "已认证"
						elsif user_detail.validate_type == 1
							validate_type = "审核中"
						else
							validate_type = "未认证"
						end
						{
							code: '0',
							msg: 'OK',
							user_info:{
								'user_id': user.id,
								'user_name': user.name.nil? ? "" : user.name,
								'gender': gender,
								'phone': user.phone.nil? ? "" : user.phone,
								"is_admin": user.admin_status,
								'validate_type': validate_type,
								'birthday': user_detail.birthday.nil? ? "" : CommonApi::format_time(user_detail.birthday),
								'ethnicity': user_detail.ethnicity.nil? ? "" : CommonApi::cover_ethnicity(user_detail.ethnicity),
								'native_place': user_detail.native_place.nil? ? "" : CommonApi::cover_city(user_detail.native_place),
								'job': user_detail.job_id.nil? ? "" : CommonApi::cover_job(user_detail.job_id),
								'company': company_name,
								'score': score,
								'experience': user_detail.experience.nil? ? "" : user_detail.experience,
								'head_image': user_detail.head_img_url.nil? ? "" : user_detail.head_img_url,
								'project_id': user_detail.project_id.nil? ? -1 : user_detail.project_id,
								'project_name': project_name,
								'pay_card': user_detail.pay_card.nil? ? "" : user_detail.pay_card
							}
						}
					else
						{code: "400", msg: "保存失败"}
					end
				end
			end
		end

		# 得分详情接口
		# 接口编号:2.1.11
		# 参数:
		## user_id  type: String 用户ID
		## page       type:Integer 页码
		# 请求方式: post
		# 返回: 工作ID
		params do
			optional :user_id, type: String, desc: 'yonghudi'
			optional :page, type: Integer, desc: '工作内容'
		end
		get 'score_detail' do 
			if params[:user_id].nil?
				{code: "400", msg: "缺少用户ID"}
			else
				per_page = 10
					result = Hash.new
					result["code"] = "0"
					result["msg"] = "OK"
					evaluations_users_for_score = EvaluationUser.where("user_id = ? and user_type = 3", params[:user_id])
					scores = 0 
					scores_num = 0
					evaluations_users_for_score.each do |eufc|
						evaluation = Evaluation.where(id: eufc.evaluation_id).first
						if evaluation.verified_type == 2
							scores = scores + evaluation.score.to_i
							scores_num = scores_num + 1
						end
					end
					if scores_num !=0
						score_aver = scores/scores_num
					else
						score_aver = -1
					end
					
					if params[:page].nil? || params[:page] == 1
						evaluations_users = EvaluationUser.where("user_id = ? and user_type = 3", params[:user_id]).order(updated_at: :desc).limit(per_page)
						evaluations_count = EvaluationUser.where("user_id = ? and user_type = 3", params[:user_id]).order(updated_at: :desc).limit(per_page).count
						result["evaluations"] = ProjectApi.build_evaluation01(evaluations_users)
						result["socre"] = score_aver
						result["count"] = scores_num
						result["previous_page"] = ""
						if (scores_num) > per_page
							result["next_page"] = "2"
						else
							result["next_page"] = ""
						end
						result
					else
						offset = (params[:page].to_i - 1) * per_page
						evaluations_users = EvaluationUser.where("user_id = ? and user_type = 3", params[:user_id]).order(updated_at: :desc).limit(per_page).offset(offset)
						evaluations_count = EvaluationUser.where("user_id = ? and user_type = 3", params[:user_id]).order(updated_at: :desc).limit(per_page).offset(offset).count
						result["evaluations"] = ProjectApi.build_evaluation01(evaluations_users)
						result["socre"] = score_aver
						result["count"] = evaluations_count
						result["previous_page"] = (params[:page].to_i - 1).to_s
						if (scores_num) > (params[:page].to_i * per_page)
							result["next_page"] = params[:page].to_i + 1
						else
							result["next_page"] = ""
						end
						result
					end
			end
		end
		#######修改用户手机号接口
		desc '修改手机号'
		params do
			optional :user_name, type: String, desc: '登录用户名(手机号/身份证号)'
			optional :passwd, type: String, desc: '密码'
			optional :sms_code, type: String, desc: '短信验证码'
			optional :phone, type:String, desc: '手机号'
		end
		post 'modify_phone' do
			if params[:user_name].nil?
				{code: '400', msg: '请输入手机号或身份证号'}
			elsif params[:passwd].nil?
				{code: '400', msg: '请输入密码'}
			elsif params[:sms_code].nil?
				{code: '400', msg: '请输入验证码'}
			else
				if params[:sms_code].eql?(Rails.cache.read(params[:phone]).to_s)
					Rails.cache.delete(params[:phone])
					passwd = Digest::MD5.hexdigest(CommonApi::des_decrypt(params[:passwd]))
					if (/(^\d{15}$)|(^\d{17}([0-9]|X))$/ =~ params[:user_name].to_s) == 0
						user = User.where(identity_card: identity_card, encrypted_password: passwd).first
						# user = User.where(identity_card: identity_card).first
					else
						user = User.where(phone: params[:user_name], encrypted_password: passwd).first
						# user = User.where(phone: params[:user_name]).first
					end
					if user.nil?
						{code: '400', msg: '用户名或密码错误'}
					else
						user.phone = params[:phone]
					end
					if user.save
						{code: '0', msg: 'OK'}
					else
						{code: '0', msg: '更新失败'}
					end
				else
					{code: '400', msg: '短信验证码错误'}
				end
			end
		end
		desc '修改密码接口'
		params do
			optional :user_name, type: String, desc: '登录用户名(手机号/身份证号)'
			optional :old_passwd, type: String, desc: '老密码'
			optional :new_passwd, type: String, desc: '新密码'
			optional :confirm_passwd, type:String, desc: '确认'
		end
		post 'modify_passwd' do
			if params[:user_name].nil?
				{code: '400', msg: '请输入手机号或身份证号'}
			elsif params[:old_passwd].nil?
				{code: '400', msg: '请输入原密码'}
			elsif params[:new_passwd].nil?
				{code: '400', msg: '请输入新密码'}
			elsif params[:confirm_passwd].nil?
				{code: '400', msg: '请输入密码确认'}
			elsif !(params[:new_passwd].eql?(params[:confirm_passwd]))
				{code: '400', msg: '密码和确认不一致'}		
			else
				user_id = UserAPI.user_login(params[:user_name], params[:old_passwd])
				if user_id == -1
					{code: "400", msg: "用户名和密码错误"}
				else
					user = User.where(id: user_id).first
					passwd = CommonApi::des_decrypt(params[:new_passwd])
					user.encrypted_password = Digest::MD5.hexdigest(passwd)
					if user.save
						{code: "0", msg: "OK"}
					else
						{code: "400", msg: "更新失败"}
					end
				end
			end
		end
		desc '实名认证接口'
		params do
			optional :user_name, type: String, desc: '用户名'
			optional :passwd, type: String, desc: '密码'
			optional :name, type: String, desc: '密码'
			optional :gender, type: String, desc: '密码'
			optional :birthday, type: String, desc: '生日'
			optional :identification_card, type: String, desc: '密码'
			optional :licensing_organization, type: String, desc: '发证机关'
			optional :ethnicity, type: String, desc: '民族'
			optional :address, type: String, desc: '住址'
			optional :job, type: String, desc: '工种'
			optional :bad_history, type: String, desc: '重大病史，1是，0否'
			optional :positive_img_url, type: String, desc: '身份证正面照'
			optional :negative_img_url, type: String, desc: '身份证反面照'
			optional :work_type, type:String, desc: '工人类型'
			optional :politics_type, type:String, desc: '政治面貌'
			optional :culture_leve, type:String, desc: '文化程度'
		end
		post 'update_real_name' do
			if params[:user_name].nil? || params[:positive_img_url].nil? || params[:negative_img_url].nil?
				{code: '400', msg: '参数错误'}
			elsif params[:passwd].nil?
				{code: '400', msg: '缺少密码'}
			else
				user_status = CommonApi.verify_login(params[:user_name], params[:passwd])
				if user_status == -1
					{code: '400', msg: '用户名或密码错误'}
				else
					user = User.find(user_status)
					user.name = params[:name]
					user.identity_card = CommonApi::des_decrypt(params[:identification_card])
					user_detail = UserDetail.where(user_id: user_status).first
					user_detail.gender = params[:gender]
					user_detail.birthday = params[:birthday]
					user_detail.bad_history = params[:bad_history].nil? ? 0 : params[:bad_history]
					user_detail.licensing_organizations = params[:licensing_organization]
					user_detail.ethnicity = params[:ethnicity]
					user_detail.address = params[:address]
					user_detail.job_id = params[:job]
					user_detail.worker_type = params[:work_type]
					user_detail.politics_type = params[:politics_type]
					user_detail.culture_leve = params[:culture_leve]
					user_detail.certification_img = params[:positive_img_url] + ";" + params[:negative_img_url]
					user_detail.real_name_status = 1
					user_detail.save 
					user.save
					{code: '0', msg: 'OK', real_name_status: '1'}
				end					
			end
		end


		desc '查询实名认证接口'
		params do
			optional :user_id, type: String, desc: '用户ID'
		end
		get 'query_real_name' do
			user = User.find(params[:user_id])
			user_detail = UserDetail.where(user_id: params[:user_id]).first
			positive_img_url = ""
			negative_img_url = ""
			if user_detail.certification_img.nil?
			else
				positive_img_url = user_detail.certification_img.split(';')[0]
				negative_img_url = user_detail.certification_img.split(';')[1]
			end

			if user_detail.certification_img.nil?
				user_detail.real_name_status = 0
				real_name_status = 0
				user_detail.save 
			else
				real_name_status = user_detail.real_name_status
			end
			{
			    code: "0",
			    msg: "OK",
			    name: user.name,
			    gender: user_detail.gender,
			    birthday: user.identity_card.nil? ? "" : CommonApi::format_time(user_detail.birthday),
			    licensing_organization: user_detail.licensing_organizations.nil? ? "" : user_detail.licensing_organizations,
			    identification_card: user.identity_card.nil? ? "" : CommonApi::des_encrypt(user.identity_card),
			    ethnicity: user_detail.ethnicity,
			    address: user_detail.address.nil? ? "" : user_detail.address,
			    bad_history: user_detail.bad_history,
			    job: user_detail.job_id,
			    work_type: user_detail.worker_type.nil? ? "" : CommonApi.cover_worker_type_to_name(user_detail.worker_type),
			    politics_type: user_detail.politics_type.nil? ? "" : CommonApi.cover_politics_type_to_name(user_detail.politics_type),
			    culture_leve: user_detail.culture_leve.nil? ? "" : CommonApi.cover_culture_leve_to_name(user_detail.culture_leve),
			    work_type_id: user_detail.worker_type.nil? ? 10 : user_detail.worker_type,
			    politics_type_id: user_detail.politics_type.nil? ? 13 : user_detail.politics_type,
			    culture_leve_id: user_detail.culture_leve.nil? ? 1 : user_detail.culture_leve,
			    positive_img_url: positive_img_url,
			    negative_img_url: negative_img_url,
			    real_name_status: real_name_status,
			    company_validate_type:user_detail.company_validate_type
			}
		end

		################方法##################
		def self.user_login(user_name, passwd)
			password = Digest::MD5.hexdigest(CommonApi::des_decrypt(passwd))
			if (/(^\d{15}$)|(^\d{17}([0-9]|X))$/ =~ user_name.to_s) == 0
				user = User.where(identity_card: user_name, encrypted_password: password).first
				if user.nil?
					return -1
				# elsif user.admin_status == 1
				# 	return -99
				else		
					return user.id
				end
			elsif (/^1[345678]\d{9}$/ =~ user_name.to_s) == 0 # 验证手机号码是否正确
				user = User.where(phone: user_name, encrypted_password: password).first
				if user.nil?
					return -1
				else
					return user.id
				end
			else
				return -1
			end
		end
		def self.user_login_new(user_name, passwd)
			if (/(^\d{15}$)|(^\d{17}([0-9]|X))$/ =~ user_name.to_s) == 0
				user = User.where(identity_card: user_name).first
				if user.nil?
					return -1
				# elsif user.admin_status == 1
				# 	return -99
				else		
					return user.id
				end
			elsif (/^1[345678]\d{9}$/ =~ user_name.to_s) == 0 # 验证手机号码是否正确
				user = User.where(phone: user_name).first
				if user.nil?
					return -1
				else
					return user.id
				end
			else
				return -1
			end
		end
		def self.auth_user(user_id)
			user = User.find(user_id)
			if user.admin_status == 1
				return -99
			else
				return user_id
			end
		end
		def self.user_score(user_id)
			evaluations_users_for_score = EvaluationUser.where("user_id = ? and user_type = 3", user_id)
			scores = 0 
			puts scores
			scores_num = evaluations_users_for_score.count
			evaluations_users_for_score.each do |eufc|
				evaluation = Evaluation.where(id: eufc.evaluation_id).first
				scores = scores + evaluation.score.to_i
			end
			if scores_num !=0
				score_aver = scores/scores_num
			else
				score_aver = -1
			end
			return score_aver
		end
end
